package com.zhq.blog.core.domain.user;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import com.zhq.blog.core.domain.BasePersistentDomain;

@Entity
public class User extends BasePersistentDomain<UserRepository> {
	
	private long userID;     //用户ID，10000起始
	private String email;    //邮箱
	private String password; //密码
	private Date lastTime;   //最后登录时间
	private String loginIP;//登录IP
	private long experience;//用户经验
	private long nums;//兑奖数
	private int isVip;   //是否为VIP 1 未是 0不是
	private long coin;       //用户金币
	private long coinBank;   //金币银行
	private String headPic;  //自定义头像
	private boolean flag;//是否冻结
	private String freeze;//冻结信息
	
	public User(){}
	
	public User(long userID, String email, String password, Date lastTime,String loginIP,long nums,long experience,
			int isVip, long coin, long coinBank, String headPic,boolean flag) {
		super();
		this.userID = userID;
		this.email = email;
		this.password = password;
		this.lastTime = lastTime;
		this.isVip = isVip;
		this.coin = coin;
		this.coinBank = coinBank;
		this.headPic = headPic;
		this.flag = flag;
	}
	public long getUserID() {
		return userID;
	}
	public void setUserID(long userID) {
		this.userID = userID;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPassword() {
		return password;
	}
	
	public String getFreeze() {
		return freeze;
	}

	public void setFreeze(String freeze) {
		this.freeze = freeze;
	}

	public String getLoginIP() {
		return loginIP;
	}
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	public void setLoginIP(String loginIP) {
		this.loginIP = loginIP;
	}
	public long getExperience() {
		return experience;
	}
	public void setExperience(long experience) {
		this.experience = experience;
	}
	public long getNums() {
		return nums;
	}
	public void setNums(long nums) {
		this.nums = nums;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Date getLastTime() {
		return lastTime;
	}
	public void setLastTime(Date lastTime) {
		this.lastTime = lastTime;
	}
	public int getIsVip() {
		return isVip;
	}
	public void setIsVip(int isVip) {
		this.isVip = isVip;
	}
	public long getCoin() {
		return coin;
	}
	public void setCoin(long coin) {
		this.coin = coin;
	}
	public long getCoinBank() {
		return coinBank;
	}
	public void setCoinBank(long coinBank) {
		this.coinBank = coinBank;
	}
	public String getHeadPic() {
		return headPic;
	}
	public void setHeadPic(String headPic) {
		this.headPic = headPic;
	}
	
	public static Specification<User> idLike(final String searchWord) {
		return new Specification<User>() {
			@Override
			public Predicate toPredicate(Root<User> root,
					CriteriaQuery<?> query, CriteriaBuilder builder) {
				
				Predicate predicate = builder.conjunction();
				List<Expression<Boolean>> expressions = predicate.getExpressions();
				expressions.add(builder.equal(root.<Boolean> get("deleted"),0));
				expressions.add(builder.equal(root.<Boolean> get("flag"),true));
				if (searchWord != null) {
					expressions.add(builder.like(root.get("userID").as(String.class),"%" + searchWord + "%"));
				}
				return query.where(predicate).getRestriction();
			}
		};
	}
	
	
}
